package com.lc202407;

import java.util.ArrayList;
import java.util.List;

public class Solution821 {
    public static int[] shortestToChar(String s, char c) {
        int length = s.length();
        int[] res = new int[length];
        List<Integer> indexList = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            if (s.charAt(i) == c) {
                indexList.add(i);
            }
        }
        int size = indexList.size();
        int index = 0;
        int start = indexList.get(0);
        int end = indexList.get(0);
        for (int i = 0; i < length; i++) {
            if (index >= size) {
                start = end;
                end = indexList.get(size - 1);
            } else {
                if (i > indexList.get(index)) {
                    index++;
                    if (index >= size) {
                        end = indexList.get(size - 1);
                    } else {
                        start = end;
                        end = indexList.get(index);
                    }
                }
            }
            res[i] = Math.min(Math.abs(i - start), Math.abs(i - end));
        }
        return res;
    }

    public static void main(String[] args) {
        shortestToChar("abaa", 'b');
    }
}
